package com.yunduansing.core.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.yunduansing.core.commons.DbCommon;
import com.yunduansing.core.model.Test;
import com.yunduansing.core.service.TestService;
import com.yunduansing.core.utils.CommonUtil;

@Controller
@RequestMapping("/test")
public class TestController {
	@Autowired
	private TestService testService;
	
	@RequestMapping(method=RequestMethod.GET)
	public String index(){
		testService.testTrans(11);
		return "test/index";
	}
	
	@ResponseBody
	@RequestMapping(value="/page",method=RequestMethod.POST,produces="application/json;charset=utf-8")
	public String getPager(DbCommon.Grid grid){
		System.out.println(grid.getIndex());
		return CommonUtil.toJsonWithGson(testService.pagination(grid));
	}
	
	@RequestMapping(value="/detail",method=RequestMethod.GET)
	public ModelAndView detail(@RequestParam(value="id",required=false) int id,ModelMap m){
		if(id!=0){
			Test test=testService.getById(id);
			if(test!=null){
				return new ModelAndView("test/detail","command",test);
			}
		}
		return new ModelAndView("test/detail","command",new Test());
	}
	
	@ResponseBody
	@RequestMapping(value="/save",method=RequestMethod.POST,produces="application/json;charset=utf-8")
	public String save(@ModelAttribute("ssm") Test m){
		return CommonUtil.toJsonWithGson(testService.save(m));
	}
	
	@ResponseBody
	@RequestMapping(value="/delete",method=RequestMethod.POST,produces="application/json;charset=utf-8")
	public String delete(@ModelAttribute("ssm") List<Integer> ids){
		return CommonUtil.toJsonWithGson(testService.delete(ids));
	}
}
